package es.udc.emarketapp.http.controller.actions.clients;

import es.udc.emarketapp.http.controller.actions.purchases.*;
import es.udc.emarketapp.emarket.model.category.vo.CategoryVO;
import es.udc.emarketapp.emarket.model.categoryFacade.CategoryFacade;
import es.udc.emarketapp.emarket.model.client.vo.ClientVO;
import es.udc.emarketapp.emarket.model.country.vo.CountryVO;
import es.udc.emarketapp.emarket.model.countryFacade.CountryFacade;
import es.udc.emarketapp.emarket.model.product.vo.ProductVO;
import es.udc.emarketapp.emarket.model.productFacade.ProductFacade;
import es.udc.emarketapp.emarket.model.productFacade.vo.ProductChunkVO;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.PurchaseFacade;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.vo.OrderChunkVO;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.vo.PurchaseChunkVO;
import es.udc.emarketapp.emarket.model.purchase.vo.PurchaseVO;
import es.udc.emarketapp.emarket.model.usersFacade.UsersFacade;
import es.udc.emarketapp.emarket.util.GlobalLimits;
import es.udc.emarketapp.http.controller.session.SessionManager;
import es.udc.emarketapp.http.view.actionForms.FindClientsForm;
import es.udc.emarketapp.http.view.actionForms.FindPurchasesForm;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.exceptions.ModelException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

public class FindClientsAction extends DefaultAction {

    public ActionForward doExecute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException {

        /* Get data. */
        String idUser = SessionManager.getIdUser(request);

        FindClientsForm findForm = (FindClientsForm) form;
        String identifierType = findForm.getIdentifierType();
        String identifier = findForm.getIdentifier();



        /****TODO Por el momento solo ID */
         return doFindClientById(mapping, identifier,
                    findForm.getStartIndex(), findForm.getCount(), request);

    }

    private ActionForward doFindClientById(ActionMapping mapping,
            String identifier, int startIndex, int count,
            HttpServletRequest request) throws InternalErrorException {


        try {

            UsersFacade usersFacade = new UsersFacade();

            ClientVO clientVO = usersFacade.findClientById(identifier);

            Locale locale = getLocale(request);
            CountryFacade countryFacade = new CountryFacade();
            CountryVO country = countryFacade.findById(clientVO.getPais(), locale.getLanguage());
            clientVO.setPais(country.getNomPais());
            country = countryFacade.findById(clientVO.getPaisEnvio(), locale.getLanguage());
            clientVO.setPaisEnvio(country.getNomPais());

            request.setAttribute("client", clientVO);

            return mapping.findForward("FindClientByIdSuccess");

        } catch (ModelException e) {

            ActionMessages errors = new ActionMessages();

            errors.add("identifier",
                    new ActionMessage("ErrorMessages.client.notFound"));
            saveErrors(request, errors);

            return new ActionForward(mapping.getInput());
        }
    }

    /*
    private ActionForward doFindClientByNifCif(ActionMapping mapping,
            String identifier, int startIndex, int count,
            HttpServletRequest request) throws InternalErrorException {


        try {

            UsersFacade usersFacade = new UsersFacade();

            ClientVO clientVO = usersFacade.findClientByNifCif(identifier);

            request.setAttribute("client", clientVO);

            return mapping.findForward("FindClientByNifCifSuccess");

        } catch (ModelException e) {

            ActionMessages errors = new ActionMessages();

            errors.add("identifier",
                    new ActionMessage("ErrorMessages.purchase.notFound"));
            saveErrors(request, errors);

            return new ActionForward(mapping.getInput());
        }
    }
*/

}
